package ha.web.show_statistics;

import ha.web.database.UserId;
import ha.web.games_manager.GM;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class show_statistics
 */
@WebServlet("/show_statistics")
public class show_statistics extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public show_statistics() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	      Map<String,String[]> requestParm = request.getParameterMap();
		  PrintWriter out = response.getWriter();
	  	    String userName = request.getParameter("user");
		    String userPass = request.getParameter("pass");
		    UserId user = new UserId(userName, userPass);
			if (user.IsUserAtDB())
		    {
				out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
				out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
				out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");  
		    	out.print("<h2>Current user statistics:</h2><br>");
		    	out.print(user.GetUserStatistics(user));
			      //check if user have a game - if so - load it
			      //if no - check waiting list
		  		out.print("<br><button type=\"button\" class=\"button\" onclick=window.location=\"login\"; }>go to login</button>");
				out.print("</div>");
				out.print("</BODY></HTML>");

		      }else {
		    	  ServletContext context = getServletContext();
		    	  RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
		    	  dispatcher.forward(request, response);
		    	  
		  		}
	    	  
	}

}
